package com.fernfx.xingtan.user.presenter;

import android.os.CountDownTimer;
import android.text.TextUtils;
import android.widget.TextView;

import com.fernfx.xingtan.R;
import com.fernfx.xingtan.common.base.BaseEntity;
import com.fernfx.xingtan.common.base.BaseView;
import com.fernfx.xingtan.common.entity.user.MemberEntity;
import com.fernfx.xingtan.common.network.IRequestCallback;
import com.fernfx.xingtan.common.network.entity.NetworkOKResult;
import com.fernfx.xingtan.main.ui.MainActivity;
import com.fernfx.xingtan.user.AccountManager;
import com.fernfx.xingtan.user.contract.ForgetPwdContract;
import com.fernfx.xingtan.user.contract.RegisterContract;
import com.fernfx.xingtan.user.model.ForgetPwdModel;
import com.fernfx.xingtan.user.model.RegisterModel;
import com.fernfx.xingtan.user.ui.LoginActivity;
import com.fernfx.xingtan.utils.FastJsonUtil;
import com.fernfx.xingtan.utils.OtherUtil;
import com.fernfx.xingtan.utils.ToastUtil;

import java.util.Map;

/**
 * Created by admin on 2018/8/3.
 */

public class ForgetPwdPresenter<P extends RegisterContract.View> implements ForgetPwdContract.Presenter,IRequestCallback{
    private P P;
    private ForgetPwdContract.Model model;

    @Override
    public void init(BaseView P) {
        this.P = (P)P;
        this.model = new ForgetPwdModel();
    }

    @Override
    public void request(Map<String, Object> requestArgsMap) {
        //显示加载
        P.getActivity().showLoading();
        model.request(requestArgsMap,this);
    }

    @Override
    public void detachView() {
        if (null != model) {
            model = null;
        }
    }

    @Override
    public CountDownTimer getTimeCount(final TextView clickTv) {
        return new CountDownTimer(60000,1000) {
            private boolean flag = true;

            @Override
            public void onTick(long millisUntilFinished) {
                if (flag) {
                    clickTv.setClickable(false);
                    flag = false;
                }
                clickTv.setText(millisUntilFinished /1000+"秒");
            }

            @Override
            public void onFinish() {
                clickTv.setText("重新发送");
                clickTv.setClickable(true);
            }
        };
    }

    @Override
    public void onSuccessful(NetworkOKResult result) {
        String message = result.getMessage();
        P.getActivity().hideLoading();
        if (TextUtils.isEmpty(message)) {
            onNetworkError(result.getMessage(),500);
            return;
        }

        MemberEntity memberEntity = FastJsonUtil.fromBean(message,MemberEntity.class);
        if (OtherUtil.checkRequestStatus(memberEntity.getSuccess(),memberEntity.getStatus())) {
            //保存登录状态
            ToastUtil.showCentertoast("修改成功");
            LoginActivity.start(P.getActivity());
            P.getActivity().finish();
        }else {
            ToastUtil.showCentertoast(memberEntity.getMsg());
        }
    }

    @Override
    public void onFailure(String errorMessage, int errorCode) {
        P.getActivity().hideLoading();
        if (TextUtils.isEmpty(errorMessage)) {
            onNetworkError(errorMessage,errorCode);
            return;
        }

        final BaseEntity baseEntity = FastJsonUtil.fromBean(errorMessage, BaseEntity.class);
        if (null != baseEntity && !TextUtils.isEmpty(baseEntity.getMsg())){
            ToastUtil.showCentertoast(baseEntity.getMsg());
        }
    }

    @Override
    public void onNetworkError(String errorMessage, int errorCode) {
        P.getActivity().hideLoading();
        ToastUtil.showCentertoast(R.string.network_error);
    }
}
